package com.share.tool.mock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.tool.mock.mapper.SourceConfMapper;
import com.share.tool.mock.model.db.HttpVariable;
import com.share.tool.mock.model.db.SourceConf;
import com.share.tool.mock.model.vo.SourceConfPageReq;
import com.share.tool.mock.service.ISourceConfService;
import com.share.tool.mock.service.HttpVariableService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 触发参数配置  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-11-24
 */
@Service
public class SourceConfServiceImpl extends ServiceImpl<SourceConfMapper, SourceConf> implements ISourceConfService {
    @Resource
    private SourceConfMapper sourceConfMapper;
    @Autowired
    private HttpVariableService httpVariableService;
    private Map<Long, SourceConf> idMap;
    private Map<String, SourceConf> stepMap;
    private Map<String, SourceConf> eventAndStepMap;
    private Map<String, List<SourceConf>> eventMap;

    @Override
    public SourceConf getByStepCode(String stepCode) {
        return stepMap.get(stepCode);
    }

    @Override
    public SourceConf getByEventAndStepCode(String eventCode, String stepCode) {
        return eventAndStepMap.get(eventCode + "-" + stepCode);
    }

    @Override
    public Map<String, List<SourceConf>> getEventMap() {
        return eventMap;
    }

    @Override
    public List<SourceConf> listByEventCode(String eventCode) {
        return eventMap.get(eventCode);
    }

    @Override
    public SourceConf getByConfId(Long confId) {
        return idMap.get(confId);
    }

    @Override
    public Object init() {
        Map<Long, SourceConf> idMap_ = new HashMap<>();
        Map<String, SourceConf> stepMap_ = new HashMap<>();
        Map<String, SourceConf> eventAndStepMap_ = new HashMap<>();
        Map<String, List<SourceConf>> eventMap_ = new HashMap<>();
        List<SourceConf> sourceConfList = sourceConfMapper.selectList(Wrappers.<SourceConf>lambdaQuery().eq(SourceConf::getStatus, 1));
        for (SourceConf item : sourceConfList) {
            idMap_.put(item.getId(), item);
            stepMap_.put(item.getStepCode(), item);
            eventAndStepMap_.put(item.getEventCode() + "-" + item.getStepCode(), item);

            List<SourceConf> confList = eventMap_.get(item.getEventCode());
            if (CollectionUtils.isEmpty(confList)) {
                confList = new LinkedList<>();
                eventMap_.put(item.getEventCode(), confList);
            }
            confList.add(item);
        }
        if (stepMap_ != null) {
            stepMap = stepMap_;
        }
        if (eventAndStepMap_ != null) {
            eventAndStepMap = eventAndStepMap_;
        }
        if (eventMap_ != null) {
            eventMap = eventMap_;
        }
        if (idMap_ != null) {
            idMap = idMap_;
        }
        return sourceConfList;
    }

    @Override
    public Page pageList(SourceConfPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<SourceConf> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getStepCode())) {
            queryWrapper.like(SourceConf::getStepCode, req.getStepCode());
        }
        if (StringUtils.isNotBlank(req.getEventName())) {
            queryWrapper.like(SourceConf::getEventName, req.getEventName());
        }
        if (StringUtils.isNotBlank(req.getStepName())) {
            queryWrapper.like(SourceConf::getStepName, req.getStepName());
        }
        if (StringUtils.isNotBlank(req.getApiUrl())) {
            queryWrapper.like(SourceConf::getApiUrl, req.getApiUrl());
        }
        List<SourceConf> list = sourceConfMapper.selectList(queryWrapper);
        Page<SourceConf> page = new Page<>(list, new PageInfo<>(list).getTotal(), req);
        for (SourceConf conf : list) {
            HttpVariable byCode = httpVariableService.getByCode(conf.getMockCode());
            if (byCode != null) {
                conf.setMockParam(byCode.getValue());
            }
        }
        return page;
    }
}
